<?php

	function __autoload($class){
	    $file      =   getFilePath($class);
	
	    if (!file_exists($file)){
	            return false;
	    }
	    require_once($file);
    }
	  
	function endswith($string, $test) {
    	$strlen = strlen($string);
    	$testlen = strlen($test);
    	if ($testlen > $strlen) return false;
    	return substr_compare($string, $test, -$testlen) === 0;
	}
	
	function getFilePath($class) {
		if(endswith($class, BUSINESS_IDENTIFIER) || endswith($class, BUSINESSFACTORY_IDENTIFIER)) {
			return realpath(dirname(__FILE__)) . BUSINESS_FOLDER . $class . '.php';
		} else if(endswith($class, MYSQL_IDENTIFIER)) {
			return realpath(dirname(__FILE__)) . DATA_ACCESS_MYSQL_FOLDER . $class . '.php';
		} else if(endswith($class, DATAACCESS_IDENTIFIER) || endswith($class, DATAACCESSFACTORY_IDENTIFIER)) {
			return realpath(dirname(__FILE__)) . DATA_ACCESS_FOLDER . $class . '.php';
		} else if(endswith($class, RESOURCE_IDENTIFIER)) {
			return realpath(dirname(__FILE__)) . RESOURCES_FOLDER . $class . '.php';
		} else {
			return realpath(dirname(__FILE__)) . MODELS_FOLDER . $class . '.php';
		}
	}
	
	function loadQueryXML() {
		
		$xmlQueryFile =  new DOMDocument();
		$xmlQueryFile->Load('smartHotelQueries.config');
		
		$_SESSION['smarthotel']['xmlQueryFile'] = $xmlQueryFile;			
	}
	
	function buildQuery($strQueryName, $arrayParams = null) {
	   $xmlQueryFile = $_SESSION['smarthotel']['xmlQueryFile'];
	   
	   $query = $xmlQueryFile->getElementsByTagName($strQueryName);
	   if ($query->length > 0) {
           $strQueryValue =  $query->item(0)->nodeValue;
		   $paramPattern = "/:[A-Z0-9]*:/";
		   preg_match_all($paramPattern, $strQueryValue, $paramMatches);
		   for ($i=0, $size = sizeof($paramMatches[0]); $i < $size; $i++) {
			   $paramId = $paramMatches[0][$i];
			   $paramName = str_replace(":", "", $paramId);
			   if(!is_null($arrayParams[$paramName])) {
			   	 $strQueryValue = str_replace($paramId, $arrayParams[$paramName], $strQueryValue);
			   }
			   else {
			   		return false;
			   }
		   }
		   return $strQueryValue;
	   }
	}
	
	function formatDateMySQL($date) {
		return date_format(new DateTime($date), 'Y-m-d H:i:s');
	}
	
	function getImageFolderUrl($resource) {
		$url = "http://" . SERVER_URL . "/" . ROOT_FOLDER . "/" . IMAGES_FOLDER . "/";
		switch ($resource) {
			case 'user':
				$url .= USER_FOLDER . "/";
				break;
			case 'tour':
				$url .= TOUR_FOLDER . "/";
				break;
			case 'event':
				$url .= EVENT_FOLDER . "/";
				break;
			default:
				break;
		}
		return $url;
	}
	
	
	function getUploadedFileFolderUrl($resource) {
		$url = "http://" . SERVER_URL . "/" . ROOT_FOLDER . "/" . UPLOADED_FILES_FOLDER . "/";
		switch ($resource) {
			case 'eventitem':
				$url .= EVENT_ITEM_FOLDER . "/";
				break;
			default:
				break;
		}
		return $url;
	}
	
	function getUploadedFileFolderPath($resource) {
		$path = realpath(dirname(__FILE__)) . "/" . UPLOADED_FILES_FOLDER . "/";
		switch ($resource) {
			case 'eventitem':
				$path .= EVENT_ITEM_FOLDER . "/";
				break;
			default:
				break;
		}
		return $path;
	}
	
	function getFileExtension($fileName) {
		
		$fileNameParts = explode('.', $fileName);
		return $fileNameParts[sizeof($fileNameParts) - 1];
		
	}
	
	function getMaterialFullPath($material) {
	
		return getUploadedFileFolderPath('eventitem') . $material->getMaterialFileName();	
	}
	
	function getMaterialName($material) {
		return MATERIAL_IDENTIFIER . $material->getEventItemMaterialId() . "." .$material->getMaterialFileExtension();
	}
	
	function getMaterialFileUrl($fileName) {
		return getUploadedFileFolderUrl('eventitem') . $fileName;
	}
	
	function formatDate($date) {
		return date_format(new DateTime($date), 'd/m/Y H:i:s');
	}
	
?>